package com.cloud.pole.shop.controller.shops.admin;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;

import com.cloud.pole.shop.biz.shops.ShopShopsComponentBiz;
import com.cloud.pole.shop.model.entity.shops.ShopShopsComponent;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;
import com.cloud.pole.shop.service.shops.ShopsComponentService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/shops/adminComponent")
@Slf4j
public class ShopsComponentAdminController extends BaseController<ShopShopsComponentBiz, ShopShopsComponent> {

	@Autowired
	private ShopsComponentService shopsComponentService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiRestResponse<ShopShopsComponent> list(Integer pageNo, Integer pageSize, Integer shopId) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			PageInfo<ShopShopsComponent> pageInfo = shopsComponentService.selectPageShopAdminList(pageNo, pageSize,
					shopId);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@PostMapping("addShopComponent")
	@ResponseBody
	public ApiRestResponse<ShopShopsComponent> addShopComponent(@RequestBody ShopShopsComponent shopShopsComponent) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			shopsComponentService.addShopsComponent(token, shopShopsComponent);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@GetMapping("getComponentById/{id}")
	@ResponseBody
	public ApiRestResponse<ShopShopsComponent> getComponentById(@PathVariable("id") Integer id) {
		try {
			ShopShopsComponent slelectById = shopsComponentService.slelectById(id);
			return ApiRestResponse.success("200", slelectById);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@PutMapping("updateShopComponent")
	@ResponseBody
	public ApiRestResponse<ShopShopsComponent> updateShopComponent(@RequestBody ShopShopsComponent shopShopsComponent) {
		try {
			shopsComponentService.updateShopsComponent(shopShopsComponent);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

}
